import { xml2js } from "xml-js";
export type YjsXML = string;

export const yjsXmlMentionCollect = (xml: YjsXML) => {
  let docObject: any;
  try {
    docObject = xml2js(xml);
  } catch {
    return [];
  }

  const mentionIds: string[] = [];
  function recMention(content: any) {
    if (!content) {
      return;
    }
    if (content.name === "mention") {
      mentionIds.push(content.attributes.id);
    } else {
      content.elements?.forEach(recMention);
    }
  }
  docObject.elements.forEach(recMention);
  return mentionIds;
};
